package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.SystemNotification;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ISystemNotificationService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 游记资源控制器
 */
@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService commendService;


    @GetMapping("/commend")
    public PageInfo queryApp(TravelCommendQuery qo){
        qo.setOrderBy("tc.schedule desc");
        qo.setSchedule(true);
        List<Map> list=commendService.queryApp(qo);
        return new PageInfo(list);
    }


    //保存游记
    @PostMapping
    public Object save(Travel travel){
        JSONResult jsonResult = new JSONResult();

        try {
            travelService.saveOrUpdate(travel);
            //systemNotificationService.saveOrUpdate(systemNotification);//系统通知
        } catch (Exception e) {
            jsonResult.mark("保存错误,请稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    //获取单篇游记
    @GetMapping("/{id}")
    public Object selectByTravelId(@PathVariable Long id){
        return  travelService.selectByTravelId(id);
    }


    //修改游记
    @PutMapping("/{id}")
    public Object update(Travel travel){
        JSONResult jsonResult = new JSONResult();
        try {
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            jsonResult.mark("保存错误,请稍后再试");
            e.printStackTrace();
        }
        return jsonResult;
    }


    //获取所有游记
    @GetMapping
    public PageInfo<Travel> query(TravelQuery qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        /** tiger
         * 查询发布状态的游记
         */
        qo.setState(Travel.STATE_RELEASE);
        /** tiger
         * 查询公开状态的游记
         */
        qo.setIsPublic(true);
        List<Travel> query = travelService.query(qo);
        return new PageInfo<>(query) ;
    }

    /** qiuyanquan
     * 判断用户是否收藏该文章
     * @param travelId
     * @param userId
     * @return
     */
    @GetMapping("/collectionCount")
    public  Integer getCollectionById( Long travelId, Long userId,int type){
        return travelService.getCollectionById(travelId,userId,type);
    }


    //收藏游记
    @PostMapping("/collection")
    public JSONResult save(Long travelId,Long userId,int type) {
        travelService.save(travelId,userId,type);
        return new JSONResult();
    }



    //取消收藏
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUser().getId();
        travelService.delete(id,userId,1);
        return new JSONResult();
    }





}
